package gases.as3.librarys.compnents.ui.container
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gases.as3.librarys.compnents.skin.data.BaseSkin;
	import gases.as3.librarys.compnents.skin.data.PanelSkin;
	import gases.as3.librarys.compnents.ui.GSkinBox;
	import gases.as3.librarys.compnents.ui.GUIComepments;
	
	[Event(name="panelClick",  	type="flash.events.Event")]
	public class GPanel extends GUIComepments
	{
		public function GPanel(w:Number,h:Number)
		{
			_width = 		w;
			_height =	    h;
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			this.mouseEnabled = true;
			_background = new GSkinBox();
			
			this.addChild(_background);
			addMouseListeners();
		}
		
		override public function set skin(value:BaseSkin):void
		{
			_panelSkin = value as PanelSkin;
			super.skin = value;
			_background.source = _panelSkin.background;
		}
		
		override public function dispose():void
		{
			removeMouseListenrs();
			this.removeChild(_background);
			_background.dispose();
			_background = null;
			super.dispose();
		}
		
		private function addMouseListeners():void
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
			this.addEventListener(MouseEvent.CLICK,onClickHandler);
		}
		
		private function removeMouseListenrs():void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			this.removeEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
			this.removeEventListener(MouseEvent.MOUSE_DOWN,onClickHandler);
		}
		
		/**
		 * 移动ui 
		 * @param e
		 * 
		 */		
		private function onMouseDownHandler(e:MouseEvent):void
		{
			this.startDrag();
		}
		
		/**
		 * 停止移动ui 
		 * @param e
		 * 
		 */		
		private function onMouseUpHandler(e:MouseEvent):void
		{
			this.stopDrag();
		}
		
		/**
		 * 点击panel并发送事件，用来展示在最前面 
		 * @param e
		 * 
		 */		
		private function onClickHandler(e:MouseEvent):void
		{
			this.dispatchEvent(new Event("panelClick"));
		}
		
		private var _width:Number;
		private var _height:Number;
		private var _background:GSkinBox;
		
		private var _panelSkin:PanelSkin;
	}
}


